<?php
/**
 * Created by PhpStorm.
 * User: lyq
 * Date: 2018/12/13
 * Time: 18:03
 */
namespace app\wechat\controller;

use app\common\model\Order;
use app\common\model\Shop;
use think\Controller;
use think\Db;

class Goods extends Common
{
    //商品详情
    public function index()
    {
        try{
           if(request()->isGet()){
               $id = intval(input('id'));
               $uid = $this->currentUserId();
               $goods = Shop::goodsInfo($id,$uid,10,1);
               $group_num = count($goods['group_history']['list']);
               $this->assign('group_num',$group_num);
               $this->templateTitle($goods['title']);
               $this->assign('goods',$goods);


               $one = '';
               $parent = trim(input('parent_mobile'));
               if($parent&&$goods['is_presell']==2){
                   $parent = base64_decode($parent);
                   $pid = Db::name('user')->where('mobile',$parent)->value('id');
                   if($pid){
                       $order = Db::name('order')
                           ->alias('o')
                           ->join('__GROUP__ g','g.id=o.group_id AND g.status=0 AND g.end_time > '.time())
                           ->where('o.uid',$pid)
                           ->where('o.pay_status',1)
                           ->where('o.is_cancle','elt',0)
                           ->where('o.is_group',1)
                           ->where('o.group_status',0)
                           ->value('o.group_id');
                       if($order){
                            $one = Shop::groupDetail($order,1);
                       }
                   }

               }
               $this->assign('one',$one);

               return $this->fetch();
           }
           if(request()->isAjax()){

               $id = intval(input('id'));
               $uid = $this->currentUserId();
               $spec_id = intval(input('spec_id'));
               $num = intval(input('num'));
               $group_id = intval(input('group_id'));
               $type = intval(input('type'));
               $user = Db::name('user')->where('id',$uid)->find();
               if(!$user){
                   $this->ajaxError('账号信息有误');
               }

               if($type==1){
                    //立即购买
                   Order::nowConfirmOrder($user,$id,$spec_id,$num);
               }else{
                   //团购
                   Order::groupConfirmOrder($user,$id,$spec_id,$num,$group_id);
               }
               $this->ajaxSuccess('验证成功');
           }
        }catch (\Exception $exception){
            global $wechat_has_exception;
            $wechat_has_exception = true;
            if(request()->isAjax()){
                $this->ajaxError($exception->getMessage());
            }else{
                $this->showError($exception->getMessage());
            }
        }

    }

    //拼单列表
    public function groupList(){
        try{
            if(request()->isAjax()){
                //商品id
                $id = intval(input('id'));
                $page = intval(input('page'));
                $list = Shop::groupCanList($id,$need_number=2,$page,10,1);
                $arr['list'] = $list['list'];
                $arr['show'] = 0;
                if(count($list)>=10){
                    $arr['show'] = 1;
                }
                $this->ajaxSuccess('',$arr);
            }
        }catch (\Exception $exception){
            global $wechat_has_exception;
            $wechat_has_exception = true;
            if(request()->isAjax()){
                $this->ajaxError($exception->getMessage());
            }else{
                $this->showError($exception->getMessage());
            }
        }
    }

    //拼单详情
    public function group(){
        try{
            if(request()->isAjax()){
                //拼单id
                $group_id = intval(input('group_id'));
                $info = Shop::groupDetail($group_id,1);

                $this->ajaxSuccess('',$info);
            }
        }catch (\Exception $exception){
            global $wechat_has_exception;
            $wechat_has_exception = true;
            if(request()->isAjax()){
                $this->ajaxError($exception->getMessage());
            }else{
                $this->showError($exception->getMessage());
            }
        }
    }

    private function cityJson(){
        header("Content-type:text/html;charset=utf-8");
        $list = Db::name('region')
            ->where('parent_id',1)
            ->field('id,title as value')
            ->select();
        foreach ($list as &$item){
            $list2 = Db::name('region')
                ->where('parent_id',$item['id'])
                ->field('id,title as value')
                ->select();
            foreach ($list2 as &$items){
                $list3 = Db::name('region')
                    ->where('parent_id',$items['id'])
                    ->field('id,title as value')
                    ->select();
                $items['childs'] = $list3;
            }
            $item['childs'] = $list2;
        }
    }

    //确认订单
    public function confirm(){
        try{

            $id = intval(input('id'));
            $spec_id = intval(input('spec_id'));
            $num = intval(input('num'));
            $group_id = intval(input('group_id'));
            $type = intval(input('type'));
            $uid = $this->currentUserId();
            $user = Db::name('user')->where('id',$uid)->find();

            if(request()->isGet()){
                $this->assign('id',$id);
                $this->assign('spec_id',$spec_id);
                $this->assign('num',$num);
                $this->assign('group_id',$group_id);
                $this->assign('type',$type);

                $this->templateTitle('确认订单');
                if($type==1){
                    //立即购买
                    $info = Order::nowConfirmOrder($user,$id,$spec_id,$num);
                }else{
                    //团购
                    $info = Order::groupConfirmOrder($user,$id,$spec_id,$num,$group_id);
                }
                $this->assign('info',$info);

                return $this->fetch();
            }
            if(request()->isAjax()){
                $name = trim(input('name'));
                $mobile = trim(input('mobile'));
                $address = trim(input('address'));
                $message = trim(input('message'));
                $use_coin = trim(input('use_coin'));
                if(empty($name)){
                    $this->ajaxError('请输入收货人姓名');
                }
                if(empty($mobile)){
                    $this->ajaxError('请输入收货人联系电话');
                }
                if(empty($address)){
                    $this->ajaxError('请输入收货地址');
                }
                if($type==1){
                    //立即购买
                    $res = Order::nowSubOrder($user,$id,$spec_id,$num,$name,$mobile,$address,$message,$use_coin);
                }else{
                    //团购
                    $res = Order::groupSubOrder($user,$id,$spec_id,$num,$name,$mobile,$address,$message,$use_coin,$group_id);
                }
                $result = Order::orderPay($user,$res['order_id'],1,1);
                $this->ajaxSuccess('',$result);
            }
        }catch (\Exception $exception){
            global $wechat_has_exception;
            $wechat_has_exception = true;
            if(request()->isAjax()){
                $this->ajaxError($exception->getMessage());
            }else{
                $this->showError($exception->getMessage());
            }
        }
    }
}